/**
 * Created with IntelliJ IDEA
 * Description:
 * User: SEKI TOMOKI
 * Date: 2023-10-02
 * Time: 17:02
 */
public class MySingleLinkedList implements IList{

    // 节点的内部类
    static class ListNode {
        public int value;
        public ListNode next;

        public ListNode(int val) {
            this.value = val;
        }
    }

    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);

        node1.next =  node2;
        node2.next =  node3;
        node3.next =  node4;

        this.head = node1;
    }

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;//把此时head的引用赋值给node节点中的next域
        this.head = node;//再让head指向node
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        if(this.head == null) {
            this.head = node;
        }else {
            ListNode cur = this.head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        if(index < 0 || index > size()) {
            //抛出自定义异常
            return;
        }

        if(index == 0) {
            addFirst(data);
        }else if(index == size()) {
            addLast(data);
        }else {
            ListNode cur = searchPrev(index);
            ListNode node = new ListNode(data);

            node.next = cur.next;
            cur.next = node;
        }
    }

    //找到index前面一个节点并返回
    private ListNode searchPrev(int index) {
        ListNode cur = this.head;
        while(0 != index - 1) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if(key == cur.value) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    @Override
    public void remove(int key) {
        if(this.head == null) {
            //一个节点都没有 无法删除
        }

        if(this.head.value == key) {
            this.head = this.head.next;
            return;
        }

        ListNode cur = findPrev(key);

        if(cur == null) {
            System.out.println("找不到该数据");
            return;
        }else {
            //cur.next = cur.next.next;
            ListNode del = cur.next;
            cur.next = del.next;
        }

    }

    private ListNode findPrev(int key) {
        ListNode cur = this.head;
        while(cur.next != null) {
            if(cur.next.value == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    @Override
    public void removeAllKey(int key) {
        //删除所有val值为key的节点
        if(this.head == null) {
            return;
        }

        ListNode cur = this.head.next;
        ListNode prev = this.head;
        //该循环没有判断第一个节点，所以循环过后要额外判断第一个节点的情况
        while(cur != null) {
            if(cur.value == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }

        //考虑第一个节点的val域是否与key相同
        if(this.head.value == key) {
            this.head = this.head.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = this.head;

        while(cur != null) {
            ListNode curNext = cur.next;
            //如果是引用数据类型 ： cur.value = null;
            cur.next = null;
            cur = curNext;
        }

        //头节点还没有置空
        head = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
